const Db = require('./db')
const vk = require('./vk')
const md5 = require('md5')
const jwt = require('jsonwebtoken')
const config = require('../config')

class admin
{
  static async login(ctx){
    let params = ctx.request.body
    // 查找账号是否存在
    let data = {
      sql:'SELECT * from vk_admin WHERE username=?',
      values:[params.username]
    }
    let [err,res] = await Db.query(data)
    if(!err){
      if(JSON.stringify(res) != '[]'){
        // 验证密码
        let password = md5(params.password)
        if(password === res[0].password){
          // 配置最后登入时间
          let lasttime = (new Date()).valueOf()
          let data2 = {
            sql:'UPDATE vk_admin SET lasttime=? WHERE username=?',
            values:[lasttime,params.username]
          }
          let [err2,res2] = await Db.query(data2)
          if(!err2){
            delete res[0].password // 删除密码输出
            res[0].token = jwt.sign({ // 生成token
              username:params.username,
              iss:"vkadmin",
              exp:Math.floor(Date.now() / 1000) + (config.outTime) //有效时间
            },'adminToken')
            ctx.body = vk.showData(200,res,'ok')
          }
        }else{
          ctx.body = vk.showData(400,[],'密码错误')
        }
      }else{
        ctx.body = vk.showData(400,[],'此账号不存在')
      }
    }else{
      ctx.body = vk.showData(500,err,'err')
    }
  }

  /**
   *
   * 修改管理员信息
   * @static
   * @memberof admin
   */
  static async edit(ctx){
    let params = ctx.request.body
    let rule_id = params.rule_id?escape(params.rule_id):''
    let data = {
      sql:"UPDATE vk_admin SET pic=?,nickname=?,password = (CASE WHEN ? != '' THEN ? ELSE password END),rule_id = (CASE WHEN ? != '' THEN ? ELSE rule_id END) WHERE username = ?",
      values:[params.pic,params.nickname,params.password,md5(params.password),rule_id,rule_id,params.username]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    let data2 = {
      sql:"SELECT * FROM vk_admin WHERE username = ?",
      values:[params.username]
    }
    let [err2,res2] = await Db.query(data2)
    if(err2){return ctx.body = vk.showData(500,err2,'请求失败')}
    ctx.body = vk.showData(200,res2,'修改成功')
  }

  /**
   *
   * 获取admin列表
   * @static
   * @memberof admin
   */
  static async list(ctx){
    let data = {
      sql:"SELECT a.id,a.username,a.nickname,a.lasttime,a.pic,a.rule_id,r.title AS rule_title,r.rules FROM vk_admin a LEFT JOIN vk_admin_authority_rule r ON a.rule_id = r.id AND r.is_use = 1"
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  /**
   *
   * 添加管理员
   * @static
   * @memberof admin
   */
  static async add(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_admin (username,nickname,pic,rule_id,password) VALUES (?,?,?,?,?)",
      values:[params.username,params.nickname,params.pic,params.rule_id,md5(params.password)]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,'请求失败')}
    ctx.body = vk.showData(200,res,'ok')
  }

  /**
   *
   * 删除管理员
   * @static
   * @memberof admin
   */
  static async del(ctx){
    let params = ctx.query
    let data = {
      sql:"DELETE FROM vk_admin WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }
}

module.exports = admin